//
//  RHAddSystemCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/12/31.
//

#import "RHAddSystemCell.h"

@interface RHAddSystemDeviceItemCell()

@property (nonatomic ,strong) QMUILabel *deviceIdLab;

@end
@implementation RHAddSystemDeviceItemCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setDeviceId:(NSString*)deviceId 
{
    self.deviceIdLab.text = deviceId;
   
}

- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = UIColor.whiteColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    [bgView rounded:8];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(3,15, 3, 15));
        make.height.mas_equalTo(38);
    }];
    
    _deviceIdLab = [[QMUILabel alloc] init];
    [_deviceIdLab setTextAlignment:NSTextAlignmentLeft];
    _deviceIdLab.numberOfLines = 0;
    [_deviceIdLab setFont:[UIFont systemFontOfSize:14]];
    [_deviceIdLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_deviceIdLab];
  
    [_deviceIdLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
    }];
}

@end




//================================================================================



@interface RHAddSystemCell()<QMUITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic ,strong) UIImageView *arrowImg;

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUITextField *textField;

@property (nonatomic, strong) QMUIButton *btn;

@end
@implementation RHAddSystemCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHAddCommonModel *)model
{
    _model = model;
    if (model.isRequired) {
        NSString *titleStr = [@"*" stringByAppendingString:model.title];
        self.titleLab.attributedText = [NSString attributeStringWithContent:titleStr attriFont:kSYSTEMFONT(14) attriColor:kCOLOR_HEX(@"#F95454") attriKey:@"*"];
    }else{
        self.titleLab.text = model.title;
    }
    self.textField.placeholder = model.placeHolder;
    self.textField.text = model.content;
    self.arrowImg.hidden = !model.isSelection;
    self.btn.hidden = !model.isSelection;
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(self.model.deviceList.count*44);
    }];
    [self.tableView reloadData];
}

-(void)setCornerRadius
{
    self.bgView.layer.cornerRadius = 8;
    self.bgView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    self.bgView.layer.masksToBounds = YES;
}


- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    
    _titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"3F4443") textAligment:NSTextAlignmentLeft];
    _titleLab.numberOfLines = 0;
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.width.mas_equalTo(kScaleW(100));
    }];
    
    UIImageView *arrowImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:arrowImg];
    self.arrowImg = arrowImg;
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
    }];
    
    
    _textField = [[QMUITextField alloc] init];
    [_textField setTextAlignment:NSTextAlignmentRight];
    [_textField setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8]];
    [_textField setFont:[UIFont systemFontOfSize:14]];
    [_textField setPlaceholder:@"Please input"];
    _textField.delegate = self;
    [bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
        make.right.equalTo(arrowImg.mas_left).offset(-5);
        make.height.mas_equalTo(30);
        make.centerY.mas_offset(0);
    }];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];

    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    
    
    _btn = [[QMUIButton alloc]init];
    [bgView addSubview:_btn];
    [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 15, 10, 15));
    }];
    
    [[_btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.clickBlock){
            weakSelf.clickBlock();
        }
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHAddSystemDeviceItemCell class] forCellReuseIdentifier:@"RHAddSystemDeviceItemCell"];
    _tableView.scrollEnabled = NO;
    [self.contentView addSubview:_tableView];
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(bgView.mas_bottom).offset(0);
        make.bottom.mas_offset(-0);
        make.height.offset(0);
    }];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.model.deviceList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHAddSystemDeviceItemCell *itemCell = [tableView dequeueReusableCellWithIdentifier:@"RHAddSystemDeviceItemCell"];
    [itemCell setDeviceId:self.model.deviceList[indexPath.row]];
    return itemCell;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.model.content = textField.text;
}

@end
